package test.file;

import static test.file.ReportProcessorConstant.ELEMENT_TYPE;
import static test.file.ReportProcessorConstant.ELEMENT_TYPE_CONSTANT;
import static test.file.ReportProcessorConstant.ELEMENT_TYPE_METHOD;
import static test.file.ReportProcessorConstant.FILE_FULL_PATH;
import static test.file.ReportProcessorConstant.LINE_NO;
import static test.file.ReportProcessorConstant.METHOD_NAME;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.bag.HashBag;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor;

public class ReportProcessor {
	
	
	private final ReportInfoReader reportReader;
	private final String reportFileFullPath;
	private final ReportLogicalWriter reportLogicalWriter;
	
	
	public ReportProcessor(final ReportInfoReader reportReader,ReportLogicalWriter reportLogicalWrite,final String reportFileFullPath) {
		super();
		this.reportReader = reportReader;
		this.reportFileFullPath = reportFileFullPath;
		this.reportLogicalWriter = reportLogicalWrite;
	}

	public  void processReport(String encoding, String rootPath) throws IOException, Exception {
		// 读取报告
		System.out.println(">>>>>>>>>> Read Report <<<<< <<<<<");
		File reportFile = new File(reportFileFullPath);
		List<Map> readReport = reportReader.readReport(reportFile, encoding, rootPath);
		// 整理报告信息
		System.out.println(">>>>>>>>>> Process Report <<<<< <<<<<");
		MultiValueMap<String, String> orgnizedReport = reportReader.orgnizeReport(readReport,rootPath);
		
		//处理报告
		reportLogicalWriter.write(orgnizedReport, encoding);
	}
}
